﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Data;
using System.Transactions;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using System.Web.SessionState;
using HKEA.CMS.Core.BLL.Tbl;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// UpDateAddressInfo 的摘要说明
    /// </summary>
    public class UpDateAddressInfo : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string strType= context.Request.QueryString["strType"].ToString();
            string str = context.Request.QueryString["datasource"].ToString();
            int ToInphasedUID = 0;
            bool ret = false;
            if (int.TryParse(context.Request["AddUID"], out ToInphasedUID) && !string.IsNullOrEmpty(str))
            {
                if (ToInphasedUID > 0)
                {
                    tblAddress addressENT = new tblAddress();
                    tblAddressBLL addressBLL = new tblAddressBLL();
                    addressENT.AddressUID = ToInphasedUID;
                    addressENT = addressBLL.GetEntity(addressENT);
                    if (addressENT != null)
                    {
                        string[] id = str.Split(',');
                        List<int> uidCP = new List<int>();
                        List<int> uidBranch = new List<int>();
                        int customerUID = 0;
                        #region 遍历出需要被同步或被还原的ContactPersonUID、BranchUID
                        foreach (var tempid in id)
                        {
                            string[] temp = tempid.Split('_');
                            if (temp[1] == "cp")
                            {
                                uidCP.Add(Convert.ToInt32(temp[0]));
                            }
                            if (temp[1] == "cust")
                            {
                                customerUID = Convert.ToInt32(temp[0]);
                            }
                            else
                            {
                                uidBranch.Add(Convert.ToInt32(temp[0]));
                            }
                        }
                        tblContactPersonBLL personBLL = new tblContactPersonBLL();
                        //得到需要被修改的ContactPerson
                        var cpList = personBLL.GetListByListID(uidCP);

                        tblCust_BranchBLL branchBLL = new tblCust_BranchBLL();
                        //得到需要被修改的Branch
                        var branchList = branchBLL.GetListByListID(uidBranch);

                        tblCustomer customerENT = new tblCustomer();
                        tblCustomerBLL customerBLL = new tblCustomerBLL();
                        if (customerUID > 0)
                        {
                            customerENT.CustomerUID = customerUID;
                            customerENT = customerBLL.GetEntity(customerENT);
                        }
                        #endregion

                        #region 同步操作
                        if (strType != "undo")
                        {
                            List<tblAddress> listToUpdate = new List<tblAddress>();
                            List<stg_tblAddress> listTostg_Address = new List<stg_tblAddress>();
                            foreach (var temp in cpList)
                            {
                                if (temp.tblAddress != null)
                                {
                                    tblAddress tempUpdate = new tblAddress();
                                    var ToStgAddress= ConvertAddressToStgAdd(temp.tblAddress);//转换成StgAddress并添加到listTostg_Address中
                                    listTostg_Address.Add(ToStgAddress);
                                    tempUpdate.AddressUID = temp.tblAddress.AddressUID;
                                    tempUpdate.Area_Chi = addressENT.Area_Chi;
                                    tempUpdate.Area_Eng = addressENT.Area_Eng;
                                    tempUpdate.City_Chi = addressENT.City_Chi;
                                    tempUpdate.City_Eng = addressENT.City_Eng;
                                    tempUpdate.CountryUID = addressENT.CountryUID;
                                    tempUpdate.District_Chi = addressENT.District_Chi;
                                    tempUpdate.District_Eng = addressENT.District_Eng;
                                    tempUpdate.Flat_Bldg_Chi = addressENT.Flat_Bldg_Chi;
                                    tempUpdate.Flat_Bldg_Eng = addressENT.Flat_Bldg_Eng;
                                    tempUpdate.FullAdd_Chi = addressENT.FullAdd_Chi;
                                    tempUpdate.FullAdd_Eng = addressENT.FullAdd_Eng;
                                    tempUpdate.LastModifyBy = GetManageName(context);
                                    tempUpdate.LastModifyDate = DateTime.Now;
                                    tempUpdate.PostalCode = addressENT.PostalCode;
                                    tempUpdate.Province_Chi = addressENT.Province_Chi;
                                    tempUpdate.Province_Eng = addressENT.Province_Eng;
                                    tempUpdate.Street_Rd_Chi = addressENT.Street_Rd_Chi;
                                    tempUpdate.Street_Rd_Eng = addressENT.Street_Rd_Eng;
                                    listToUpdate.Add(tempUpdate);
                                }
                            }
                            foreach (var temp in branchList)
                            {                                
                                if (temp.AddressUID > 0)
                                {
                                    tblAddress tempUpdate = new tblAddress();
                                    tempUpdate.AddressUID = (int)temp.AddressUID;
                                    var tempAddress = addressBLL.GetEntity(tempUpdate);//得到需要被修改的Address实体
                                    if (tempAddress != null)
                                    {
                                        var ToStgAddress = ConvertAddressToStgAdd(tempAddress);//转换成StgAddress并添加到listTostg_Address中
                                        listTostg_Address.Add(ToStgAddress);

                                        tempUpdate.Area_Chi = addressENT.Area_Chi;
                                        tempUpdate.Area_Eng = addressENT.Area_Eng;
                                        tempUpdate.City_Chi = addressENT.City_Chi;
                                        tempUpdate.City_Eng = addressENT.City_Eng;
                                        tempUpdate.CountryUID = addressENT.CountryUID;
                                        tempUpdate.District_Chi = addressENT.District_Chi;
                                        tempUpdate.District_Eng = addressENT.District_Eng;
                                        tempUpdate.Flat_Bldg_Chi = addressENT.Flat_Bldg_Chi;
                                        tempUpdate.Flat_Bldg_Eng = addressENT.Flat_Bldg_Eng;
                                        tempUpdate.FullAdd_Chi = addressENT.FullAdd_Chi;
                                        tempUpdate.FullAdd_Eng = addressENT.FullAdd_Eng;
                                        tempUpdate.LastModifyBy = GetManageName(context);
                                        tempUpdate.LastModifyDate = DateTime.Now;
                                        tempUpdate.PostalCode = addressENT.PostalCode;
                                        tempUpdate.Province_Chi = addressENT.Province_Chi;
                                        tempUpdate.Province_Eng = addressENT.Province_Eng;
                                        tempUpdate.Street_Rd_Chi = addressENT.Street_Rd_Chi;
                                        tempUpdate.Street_Rd_Eng = addressENT.Street_Rd_Eng;
                                        listToUpdate.Add(tempUpdate);
                                    }
                                }
                            }
                            if (customerENT != null)
                            {
                                if (customerENT.AddressUID > 0)
                                {
                                    tblAddress tempUpdate = new tblAddress();
                                    tempUpdate.AddressUID = (int)customerENT.AddressUID;
                                    var tempAddress = addressBLL.GetEntity(tempUpdate);//得到需要被修改的Address实体
                                    if (tempAddress != null)
                                    {
                                        var ToStgAddress = ConvertAddressToStgAdd(tempAddress);//转换成StgAddress并添加到listTostg_Address中
                                        listTostg_Address.Add(ToStgAddress);

                                        tempUpdate.Area_Chi = addressENT.Area_Chi;
                                        tempUpdate.Area_Eng = addressENT.Area_Eng;
                                        tempUpdate.City_Chi = addressENT.City_Chi;
                                        tempUpdate.City_Eng = addressENT.City_Eng;
                                        tempUpdate.CountryUID = addressENT.CountryUID;
                                        tempUpdate.District_Chi = addressENT.District_Chi;
                                        tempUpdate.District_Eng = addressENT.District_Eng;
                                        tempUpdate.Flat_Bldg_Chi = addressENT.Flat_Bldg_Chi;
                                        tempUpdate.Flat_Bldg_Eng = addressENT.Flat_Bldg_Eng;
                                        tempUpdate.FullAdd_Chi = addressENT.FullAdd_Chi;
                                        tempUpdate.FullAdd_Eng = addressENT.FullAdd_Eng;
                                        tempUpdate.LastModifyBy = GetManageName(context);
                                        tempUpdate.LastModifyDate = DateTime.Now;
                                        tempUpdate.PostalCode = addressENT.PostalCode;
                                        tempUpdate.Province_Chi = addressENT.Province_Chi;
                                        tempUpdate.Province_Eng = addressENT.Province_Eng;
                                        tempUpdate.Street_Rd_Chi = addressENT.Street_Rd_Chi;
                                        tempUpdate.Street_Rd_Eng = addressENT.Street_Rd_Eng;
                                        listToUpdate.Add(tempUpdate);
                                    }
                                }
                            }
                            ret = addressBLL.UpDateAddress(listToUpdate, listTostg_Address);
                        }
                        #endregion
                        #region 还原操作
                        else
                        {
                            List<tblAddress> listOfUpDate = new List<tblAddress>();
                            foreach (var temp in cpList)//遍历需要被还原操作的cp
                            {
                                var tempAdd = temp.tblAddress; 
                                if (tempAdd != null)//CPAddress不为null时，将tblAddress添加到被还原集合中
                                { listOfUpDate.Add(tempAdd); }
                            }
                            foreach (var temp in branchList)//遍历需要被还原操作的brandch
                            {
                                tblAddress tempAdd = new tblAddress();
                                if (temp.AddressUID > 0)  
                                {
                                    tempAdd.AddressUID = (int)temp.AddressUID;
                                    tempAdd = addressBLL.GetEntity(tempAdd);
                                    if (tempAdd != null)//CPAddress不为null时，将tblAddress添加到被还原集合中
                                    { listOfUpDate.Add(tempAdd); }
                                } 
                            }
                            if (customerENT.AddressUID > 0)
                            {
                                tblAddress tempAdd = new tblAddress();
                                tempAdd.AddressUID = (int)customerENT.AddressUID;
                                tempAdd = addressBLL.GetEntity(tempAdd);
                                if (tempAdd != null)//CusomerAddress不为null时，将tblAddress添加到被还原集合中
                                { listOfUpDate.Add(tempAdd); }
                            }
                            ret = tblAddressBLL.GetInstance().UpDateAddress(listOfUpDate);
                        }
                        #endregion
                    }
                }
            }
            if (ret)
            {
                context.Response.Write("1");
            }
            else
            { context.Response.Write("0"); }
            context.Response.Flush(); 
        }
        public string GetManageName(HttpContext context)
        {
            string ret = string.Empty;
            if (context.Session[Constant.SessionName.Manage] != null)
            {
                ret = (context.Session[Constant.SessionName.Manage] as tblUser).User_Name;
            }
            else
            { ret = "system"; }
            return ret;
        }

        public stg_tblAddress ConvertAddressToStgAdd(tblAddress address)
        {
            stg_tblAddress ret = new stg_tblAddress();
            if (address != null)
            {
                ret.AddressUID = address.AddressUID;
                ret.Area_Chi = address.Area_Chi;
                ret.Area_Eng = address.Area_Eng;
                ret.City_Chi = address.City_Chi;
                ret.City_Eng = address.City_Eng;
                ret.CountryUID = address.CountryUID;
                ret.CreationDate = address.CreationDate;
                ret.Department_Chi = address.Department_Chi;
                ret.Department_Eng = address.Department_Eng;
                ret.District_Chi = address.District_Chi;
                ret.District_Eng = address.District_Eng;
                ret.Flat_Bldg_Chi = address.Flat_Bldg_Chi;
                ret.Flat_Bldg_Eng = address.Flat_Bldg_Eng;
                ret.FullAdd_Chi = address.FullAdd_Chi;
                ret.FullAdd_Eng = address.FullAdd_Eng;
                ret.LastModifyBy = address.LastModifyBy;
                ret.LastModifyDate = address.LastModifyDate;
                ret.PostalCode = address.PostalCode;
                ret.Province_Chi = address.Province_Chi;
                ret.Province_Eng = address.Province_Eng;
                ret.Street_Rd_Chi = address.Street_Rd_Chi;
                ret.Street_Rd_Eng = address.Street_Rd_Eng;
            }
            return ret;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}